//
//  GRRootNavigationController.m
//  YCGRICE
//
//  Created by Liu Feng on 2018/7/4.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRRootNavigationController.h"
#import "Common.h"

static BOOL RootNavigationBarBackItemTitleIsExist;

@implementation GRRootNavigationController

+ (void)load {
    RootNavigationBarBackItemTitleIsExist = [Common isVariableWithClass:[UINavigationItem class] varName:@"backButtonTitle"];
}

-(instancetype)initWithRootViewController:(UIViewController *)rootViewController {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        [self configChildControllerBackTitle:@[rootViewController]];
    }
    return self;
}

-(instancetype)initWithRootViewController:(UIViewController *)rootViewController navigationBarColor:(UIColor *)color {
    self = [super initWithRootViewController:rootViewController];
    if (self) {
        self.navigationBar.barTintColor = color;
        [self configChildControllerBackTitle:@[rootViewController]];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self defaultNavigationBarStyle];
}

- (void)defaultNavigationBarStyle {
    self.navigationBar.barStyle     = UIBarStyleDefault;
    self.navigationBar.translucent  = YES;
    self.navigationBar.barTintColor = [UIColor whiteColor];
    self.navigationBar.titleTextAttributes = @{NSFontAttributeName : [UIFont systemFontOfSize:18], NSForegroundColorAttributeName : HEXCOLOR(0x222222)};
    [[UIBarButtonItem appearance] setTitleTextAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:15], NSForegroundColorAttributeName : HEXCOLOR(0x222222)} forState:UIControlStateNormal];
    UIImage *backImageOriginal = [UIImage imageNamed:@"backIndicator"];
    UIImage *backImage = [[self image:backImageOriginal leftOffset:7] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
    [self.navigationBar setBackIndicatorImage:backImage];
    [self.navigationBar setBackIndicatorTransitionMaskImage:backImage];
    
    
    [self.navigationBar setBackgroundImage:nil
                            forBarPosition:UIBarPositionAny
                                barMetrics:UIBarMetricsDefault]; // 设置navigationBar的颜色为透明的
    
    [self.navigationBar setShadowImage:[UIImage new]];
    
    // 去掉黑线
    [self removeBlackLine];
}

- (void)removeBlackLine {
    if (self.navigationBar.subviews.count > 0) {
        NSArray *subviews = self.navigationBar.subviews[0].subviews;
        for (UIView *view in subviews) {
            if ([view isKindOfClass:[UIImageView class]] && view.height <= 1) {
                view.hidden = YES;
            }
        }
    }
}


- (UIImage *)image:(UIImage*)image leftOffset:(CGFloat)leftOffset {
    UIImage *sourceImage = image;
    UIImage *newImage = nil;
    
    CGSize size = CGSizeMake(image.size.width + leftOffset, image.size.height);
    
    UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
    
    CGRect thumbnailRect = CGRectZero;
    thumbnailRect.origin = CGPointMake(leftOffset, 0);
    thumbnailRect.size.width  = size.width - leftOffset;
    thumbnailRect.size.height = size.height;
    
    [sourceImage drawInRect:thumbnailRect];
    
    newImage = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    
    return newImage ;
}

-(void)setViewControllers:(NSArray<__kindof UIViewController *> *)viewControllers {
    
    [self hideBottomBarWhenPush:viewControllers];
    
    [super setViewControllers:viewControllers];
}

-(void)setViewControllers:(NSArray<UIViewController *> *)viewControllers animated:(BOOL)animated {
    
    [self hideBottomBarWhenPush:viewControllers];
    
    [super setViewControllers:viewControllers animated:animated];
}


-(void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [self configChildControllerBackTitle:@[viewController]];
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
    }
    
    [super pushViewController:viewController animated:animated];
}

-(void)showViewController:(UIViewController *)vc sender:(id)sender {
    [self configChildControllerBackTitle:@[vc]];
    if (self.viewControllers.count > 0) {
        vc.hidesBottomBarWhenPushed = YES;
    }
    
    [super showViewController:vc sender:sender];
}

/**
 push时隐藏bottomBar

 @param viewControllers <#viewControllers description#>
 */
- (void)hideBottomBarWhenPush:(NSArray<UIViewController *> *)viewControllers {
    [self configChildControllerBackTitle:viewControllers];
    
    if (viewControllers && viewControllers.count > 0) {
        
        for (UIViewController *viewcontroller in viewControllers) {
            viewcontroller.hidesBottomBarWhenPushed = YES;
        }
    }
}

/**
 配置viewController返回按钮的title
 */
-(void)configChildControllerBackTitle:(NSArray<__kindof UIViewController*> *) viewControllers {
    
    if(!viewControllers || viewControllers.count == 0) {
        return;
    }
    
    if(RootNavigationBarBackItemTitleIsExist) {
        for (UIViewController *controller in viewControllers) {
            
            [controller.navigationItem setValue:@"" forKey:@"backButtonTitle"];
        }
    }
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
